local caiqi = fk.CreateSkill{
    name = "ym1__caiqi",
}
Fk:loadTranslationTable{
    ["ym1__caiqi"] = "才绮",
    [":ym1__caiqi"] = "每回合每项各限一次，你使用非装备牌结算完成后，可：1.横置；2.明置手牌；3.翻至背面；4.重铸一个区域。若本回合均执行，你可<a href='ym1__caiqi-adaptation'>视为使用【随机应变】</a>。",
    ["ym1__caiqi-adaptation"] = "<b>视为使用【随机应变】</b>：实际上就是视为使用你本回合使用或打出的上一张基本牌或普通锦囊牌。",
    ["#ym1__caiqi-choice1"] = "横置",
    ["#ym1__caiqi-choice2"] = "明置手牌",
    ["#ym1__caiqi-choice3"] = "翻至背面",
    ["#ym1__caiqi-choice4"] = "重铸一个区域",
    ["@ym1__caiqi-turn"] = "才绮",
    ["#ym1__caiqi"] = "是否发动 才绮，执行以下一项？",
    ["#ym1__caiqi-recast"] = "才绮：重铸一个区域",
    ["#ym1__caiqi-use"] = "才绮：视为使用【随机应变】（%arg）",
}
caiqi:addEffect(fk.CardUseFinished,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(caiqi.name) and target == player and data.card.type ~= Card.TypeEquip then
            return type(player:getMark("ym1__caiqi-turn")) ~= "table" or #player:getMark("ym1__caiqi-turn") < 4
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices = {}
        local mark = type(player:getMark("ym1__caiqi-turn")) == "table" and player:getMark("ym1__caiqi-turn") or {}
        if not table.contains(mark,"#ym1__caiqi-choice1") and not player.chained then
            table.insert(choices,"#ym1__caiqi-choice1")
        end
        if not table.contains(mark,"#ym1__caiqi-choice2") and player:getHandcardNum() ~= #table.filter(player:getCardIds("h"), function (id)
            if Fk:getCardById(id):getMark(MarkEnum.ShownCards) > 0 then
                return true
            end
            if table.find(MarkEnum.TempMarkSuffix, function (suffix)
            return Fk:getCardById(id):getMark(MarkEnum.ShownCards..suffix) > 0
            end) then
                return true
            end
            return false
        end) then
            table.insert(choices,"#ym1__caiqi-choice2")
        end
        if not table.contains(mark,"#ym1__caiqi-choice3") and player.faceup then
            table.insert(choices,"#ym1__caiqi-choice3")
        end
        if not table.contains(mark,"#ym1__caiqi-choice4") and not player:isAllNude() then
            table.insert(choices,"#ym1__caiqi-choice4")
        end
        if #choices > 0 then
            table.insert(choices,"Cancel")
            local result = room:askToChoice(player,{
                choices = choices,
                skill_name = caiqi.name,
                prompt = "#ym1__caiqi",
                all_choices = {"#ym1__caiqi-choice1","#ym1__caiqi-choice2","#ym1__caiqi-choice3","#ym1__caiqi-choice4","Cancel"},
            })
            if result ~= "Cancel" then
                event:setCostData(self,result)
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)
        local mark = type(player:getMark("ym1__caiqi-turn")) == "table" and player:getMark("ym1__caiqi-turn") or {}
        table.insert(mark,choice)
        room:setPlayerMark(player,"ym1__caiqi-turn",mark)
        local all_index = {" 壹"," 贰"," 叁"," 肆"}
        room:setPlayerMark(player,"@ym1__caiqi-turn",all_index[#mark])
        --room:notifySkillInvoked(player,caiqi.name)
        switch(choice,{
            ["#ym1__caiqi-choice1"] = function()
                --player:broadcastSkillInvoke(caiqi.name,1)
                player:setChainState(true)
            end,
            ["#ym1__caiqi-choice2"] = function()
                --player:broadcastSkillInvoke(caiqi.name,2)
                for _, id in ipairs(player:getCardIds("h")) do
                    room:setCardMark(Fk:getCardById(id), '@@ShownCards-inhand', 1)
                end
            end,
            ["#ym1__caiqi-choice3"] = function()
                --player:broadcastSkillInvoke(caiqi.name,3)
                player:turnOver()
            end,
            ["#ym1__caiqi-choice4"] = function()
                --player:broadcastSkillInvoke(caiqi.name,4)
                local choices, all_choices = {}, {"$Hand","$Equip","$Judge"}
                for i, v in ipairs({"h", "e", "j"}) do
                    if #player:getCardIds(v) > 0 then
                        table.insert(choices, all_choices[i])
                    end
                end
                local choice2 = room:askToChoice(player, {
                    choices = choices,
                    skill_name = caiqi.name,
                    prompt = "#ym1__caiqi-recast",
                    all_choices = all_choices,
                })
                if choice2 == "$Hand" then
                    room:recastCard(player:getCardIds("h"), player, caiqi.name)
                elseif choice2 == "$Equip" then
                    room:recastCard(player:getCardIds("e"), player, caiqi.name)
                elseif choice2 == "$Judge" then
                    room:recastCard(player:getCardIds("j"), player, caiqi.name)
                end
            end,
        })

        if #mark >= 4 and player:getMark("adaptation-turn") ~= 0 then
            local c = Fk:cloneCard(player:getMark("adaptation-turn"))
            if not player:prohibitUse(c) then
                player:broadcastSkillInvoke(caiqi.name,5)
                room:askToUseVirtualCard(player,{
                    name = c.name,
                    skill_name = caiqi.name,
                    prompt = "#ym1__caiqi-use:::"..c.name
                })
            end
        end
    end,
})

local spec = {
    global = true,
    can_refresh = function(self, event, target, player, data)
        return target == player and data.card and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
    end,
    on_refresh = function(self, event, target, player, data)
        player.room:setPlayerMark(player, "adaptation-turn", data.card.name)
    end,
}
caiqi:addEffect(fk.PreCardUse, spec)
caiqi:addEffect(fk.PreCardRespond, spec)

return caiqi